package com.jingli.pile.converter;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jingli.pile.configuration.PushProperties;
import com.jingli.pile.entity.ChargingPile;
import com.jingli.pile.entity.SupEquipmentInfo;
import com.jingli.pile.entity.SupStationInfo;
import com.jingli.pile.service.SupStationInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
public class EquipmentConverter {
    private Logger logger = LoggerFactory.getLogger(EquipmentConverter.class);
    @Autowired
    private SupStationInfoService stationInfoService;
    @Autowired
    private PushProperties pushProperties;

    public ChargingPile convertEquipmentToPile(SupEquipmentInfo equipmentInfo){
        ChargingPile chargingPile = new ChargingPile();
        chargingPile.setId(equipmentInfo.getEquipmentid());
        chargingPile.setPkname(equipmentInfo.getEquipmentname());
        chargingPile.setCpFeeType("2001");
        chargingPile.setSimCard("88888888888888");
        chargingPile.setSimDueTime(DateUtil.formatDateTime(new Date()));
        chargingPile.setRunningStatus(0);
        //todo find connector
        chargingPile.setUserid("");
        //todo find plot
        chargingPile.setPlot("");
        //todo find rule
        chargingPile.setWxRuleId(0);
        chargingPile.setCreateTime(new Date());
        chargingPile.setDeviceType(4);
        chargingPile.setPileType(equipmentInfo.getEquipmentType());
        chargingPile.setPileStatus(1);
        chargingPile.setElectricity(String.valueOf(equipmentInfo.getCurrent()));
        chargingPile.setVoltage(String.valueOf(equipmentInfo.getVoltageupperlimits()));
        chargingPile.setMaxPower(equipmentInfo.getPower().intValue());
        chargingPile.setFeeNeedUpdate(0);
        return chargingPile;
    }

    public SupEquipmentInfo convertEquipFromPile(ChargingPile chargingPile){
        SupEquipmentInfo equipmentInfo = new SupEquipmentInfo();
        equipmentInfo.setEquipmentid(chargingPile.getId());
        equipmentInfo.setSvin(1);
        equipmentInfo.setSautoPower(1);
        equipmentInfo.setEquipmentClassification(1);
        equipmentInfo.setEquipmentUniqueNumber(chargingPile.getId());
        equipmentInfo.setEquipmentType(2);
        equipmentInfo.setPower(chargingPile.getMaxPower().doubleValue());
        equipmentInfo.setManufacturerid(pushProperties.getPlatformId());
        equipmentInfo.setManufacturername("承宇");
        equipmentInfo.setCurrent(Integer.valueOf(chargingPile.getElectricity()));
        equipmentInfo.setVoltagelowerlimits(Integer.valueOf(chargingPile.getVoltage()));
        equipmentInfo.setVoltageupperlimits(Integer.valueOf(chargingPile.getVoltage()));
        QueryWrapper<SupStationInfo> stationInfoQueryWrapper = new QueryWrapper<>();
        stationInfoQueryWrapper.eq("plot_id",chargingPile.getPlot());
        SupStationInfo stationInfo = stationInfoService.getOne(stationInfoQueryWrapper);
        equipmentInfo.setStationId(stationInfo.getStationId());
        return equipmentInfo;
    }
}
